
package com.wyzz.hr.action;

import java.util.*;

import com.wyzz.hr.base.BaseDownloadAction;
import com.wyzz.hr.base.BasePdfExport;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IOuperfcriteriaBo;
import com.wyzz.hr.bo.IOuqualifyBo;
import com.wyzz.hr.bo.IOuresponseBo;
import com.wyzz.hr.bo.IPositionBaseBo;
import com.wyzz.hr.bo.IPositionBo;
import com.wyzz.hr.bo.TemplateService;
import com.wyzz.hr.pojo.Ouperfcriteria;
import com.wyzz.hr.pojo.Ouqualify;
import com.wyzz.hr.pojo.Ouresponse;
import com.wyzz.hr.pojo.Position;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.MyTools;
import com.wyzz.hr.util.SysConfigManager;

public class PbManualAction extends BaseDownloadAction {

	public PbManualAction() {
		dbConfigManager = DatabaseSysConfigManager.getInstance();
		templateName = "print_pbInfo";
	}

	public String showPbManual() {
		initParams();
		TemplateService templateService = (TemplateService) SpringBeanFactory
				.getBean("ftlTemplateService");
		if (!templateService.checkPDFTemplateExist(templateName)) {
			printContent = "\u6A21\u677F\u4E0D\u5B58\u5728\uFF01";// 模板不存在！
			return "success";
		}
		processWrap();
		Map modal = new HashMap();
		modal.put("pb", pb);
		modal.put("exceedConfig", exceedConfig);
		modal.put("pbRespList", pbRespList);
		modal.put("pbQualifyList", pbQualifyList);
		modal.put("pbPerfcrList", pbPerfcrList);
		modal.put("empNameListString", empNameListString);
		printContent = templateService.getPDFContent(templateName, modal);
		if ("pdf".equals(resultType)) {
			if (printContent.indexOf("&nbsp;") > -1)
				printContent = printContent.replaceAll("&nbsp;", "");
			String fileNameWithPath = BasePdfExport.pdfExport(printContent);
			if (download(fileNameWithPath, (new StringBuilder()).append(
					templateName).append(".pdf").toString()))
				return "download";
		}
		return "success";
	}

	public String showPositionPBInfo() {
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		Position pos = posBo.getPositionById(positionId);
		pbId = pos.getPositionPbId().getId();
		initParams();
		return "success";
	}

	private void initParams() {
		Map dbMap = dbConfigManager.getProperties();
		exceedConfig = (String) dbMap.get("sys.position.max.exceed");
		if (exceedConfig == null)
			exceedConfig = "1";
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		String fetch[] = { PositionBase.PROP_PB_DEPT_ID,
				PositionBase.PROP_PB_SUP_ID };
		pb = pbBo.loadPb(pbId, fetch);
		IEmployeeBo empBo = (IEmployeeBo) SpringBeanFactory.getBean("empBo");
		List empNameList = empBo.getPbActiveEmpNos(pb.getId());
		empNameListString = MyTools.arrayToStr(empNameList.toArray(), '\u3001');  //、
		IOuresponseBo pbRespBo = (IOuresponseBo) SpringBeanFactory
				.getBean("ouresponseBo");
		pbRespList = pbRespBo.getPbRespByPbId(pbId);
		IOuqualifyBo pbQualifyBo = (IOuqualifyBo) SpringBeanFactory
				.getBean("ouqualifyBo");
		pbQualifyList = pbQualifyBo.getpbQualifyByPbId(pbId);
		IOuperfcriteriaBo perfcrBo = (IOuperfcriteriaBo) SpringBeanFactory
				.getBean("ouperfcriteriaBo");
		pbPerfcrList = perfcrBo.getPbPerfcriteria(pbId);
	}

	private void processWrap() {
		empNameListString = BasePdfExport.processWrap(empNameListString, 680,
				10, 12);
		for (int i = 0; i < pbRespList.size(); i++) {
			Ouresponse resp = (Ouresponse) pbRespList.get(i);
			resp.setOurName(BasePdfExport.processWrap(resp.getOurName(), 500,
					10, 12));
		}

		for (int i = 0; i < pbQualifyList.size(); i++) {
			Ouqualify qualify = (Ouqualify) pbQualifyList.get(i);
			qualify.setOuqName(BasePdfExport.processWrap(qualify.getOuqName(),
					300, 10, 12));
			qualify.setOuqDesc(BasePdfExport.processWrap(qualify.getOuqDesc(),
					380, 10, 12));
		}

		for (int i = 0; i < pbPerfcrList.size(); i++) {
			Ouperfcriteria oup = (Ouperfcriteria) pbPerfcrList.get(i);
			oup.setOupName(BasePdfExport.processWrap(oup.getOupName(), 500, 10,
					12));
		}

	}

	public String getPbId() {
		return pbId;
	}

	public void setPbId(String pbId) {
		this.pbId = pbId;
	}

	public PositionBase getPb() {
		return pb;
	}

	public void setPb(PositionBase pb) {
		this.pb = pb;
	}

	public String getExceedConfig() {
		return exceedConfig;
	}

	public void setExceedConfig(String exceedConfig) {
		this.exceedConfig = exceedConfig;
	}

	public String getEmpNameListString() {
		return empNameListString;
	}

	public void setEmpNameListString(String empNameListString) {
		this.empNameListString = empNameListString;
	}

	public List getPbRespList() {
		return pbRespList;
	}

	public void setPbRespList(List pbRespList) {
		this.pbRespList = pbRespList;
	}

	public List getPbQualifyList() {
		return pbQualifyList;
	}

	public void setPbQualifyList(List pbQualifyList) {
		this.pbQualifyList = pbQualifyList;
	}

	public List getPbPerfcrList() {
		return pbPerfcrList;
	}

	public void setPbPerfcrList(List pbPerfcrList) {
		this.pbPerfcrList = pbPerfcrList;
	}

	public String getPrintContent() {
		return printContent;
	}

	public void setPrintContent(String printContent) {
		this.printContent = printContent;
	}

	public String getResultType() {
		return resultType;
	}

	public void setResultType(String resultType) {
		this.resultType = resultType;
	}

	public String getTemplateName() {
		return templateName;
	}

	public void setTemplateName(String templateName) {
		this.templateName = templateName;
	}

	public String getPositionId() {
		return positionId;
	}

	public void setPositionId(String positionId) {
		this.positionId = positionId;
	}

	public String getPageType() {
		return pageType;
	}

	public void setPageType(String pageType) {
		this.pageType = pageType;
	}

	private static final long serialVersionUID = 1L;
	private String pbId;
	private PositionBase pb;
	private String empNameListString;
	private List pbRespList;
	private List pbQualifyList;
	private List pbPerfcrList;
	private String exceedConfig;
	private SysConfigManager dbConfigManager;
	private String printContent;
	private String resultType;
	private String templateName;
	private String positionId;
	private String pageType;
}
